package com.xjtu.duansheli.controller;


import com.xjtu.duansheli.entity.ProductImagesInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.ProductImagesInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/product-images-info")
@Api(value = "商品图片信息管理模块", description = "这是商品图片信息增删改查的模块")
public class ProductImagesInfoController {

    @Autowired
    private ProductImagesInfoService productImagesInfoService;

    @ApiOperation(value = "获取全部商品图片", notes = "获取所有商品图片的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getProductsImages", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(productImagesInfoService.getAllProductImages(current, size));
    }

    @ApiOperation(value = "根据id查找商品图片", notes = "获取根据id查找到的商品图片")
    @ApiImplicitParam(name = "id", value = "商品图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductImagesById", method = RequestMethod.GET)
    public AjaxResponse getProductImageById(@RequestParam(name = "id") Long id) {
        return AjaxResponse.success(productImagesInfoService.getProductImageByid(id));
    }

    @ApiOperation(value = "根据商品id查找对应图片", notes = "获取根据商品id查找到的商品图片")
    @ApiImplicitParam(name = "pid", value = "商品id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getProductImagesBypId", method = RequestMethod.GET)
    public AjaxResponse getProductImageBypId(@RequestParam(name = "pid") Long pid) {
        return AjaxResponse.success(productImagesInfoService.getProductImageBypid(pid));
    }


    @ApiOperation(value = "添加商品图片信息", notes = "获取商品图片信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "商品图片文件", required = true, paramType = "form", dataType = "file"),
            @ApiImplicitParam(name = "pid", value = "商品id", required = true, paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "/addIdenImages", method = RequestMethod.POST)
    public AjaxResponse addIdenImage(@RequestParam(value = "files") MultipartFile[] files,
                                     @RequestParam(value = "pid") Long pid) throws IOException {
        return AjaxResponse.success("数据库受影响行数->" + productImagesInfoService.addProductImages(files, pid));
    }


    @ApiOperation(value = "删除商品图片信息", notes = "从数据库中删除某个商品图片")
    @ApiImplicitParam(name = "id", value = "删除商品图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteProductImage", method = RequestMethod.DELETE)
    public AjaxResponse deleteProductImage(@RequestParam(value = "id") Long id) {

        ProductImagesInfo productInfo = productImagesInfoService.getProductImageByid(id);
        File myDelFile = new File(productInfo.getImagePath());
        boolean delete = myDelFile.delete();
        log.info("delete file resource:" + productInfo.getImagePath());
        if (delete)
            return AjaxResponse.success("数据库受影响行数->" + productImagesInfoService.deleteProductImages(id));
        else
            return AjaxResponse.error("本地文件不存在或者删除失败！");
    }

    @ApiOperation(value = "商品图片总数量", notes = "获取数据库中商品图片总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(productImagesInfoService.getTotal());
    }
}

